package com.project.geonotes;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Category {
	 private static final String JSON_NAME_ATTRIBUTE = "category_name";
     private static final String JSON_NOTE_PLACE_ATTRIBUTE = "note_place";
     private static final String JSON_NOTE_NOTE_ATTRIBUTE = "note_note";
     private static final String JSON_NOTE_LAT_ATTRIBUTE = "note_lat";
     private static final String JSON_NOTE_LONG_ATTRIBUTE = "note_long";
     private String categoryname;
     private String[] noteplace, notenote, notelat, notelong;

     /**
      * Creates a new Contact.
      * 
      * @param name
      *            - the name of the Contact.
      * @param phoneNumber
      *            - the phone number of the Contact.
      * @param email
      *            - the email address of the Contact.
      */
     public Category(String name, String[] notePlace, String[] noteNote, String[] noteLat, String[] noteLong) {
             this.categoryname = name;
             this.noteplace = notePlace;
             this.notenote = noteNote;
             this.notelat = noteLat;
             this.notelong = noteLong;
     }

     /**
      * @return the name of this Contact
      */
     public String getName() {
             return categoryname;
     }

     /**
      * @return the phone number of this Contact
      */
     public String[] getNotePlace() {
             return noteplace;
     }

     /**
      * @return the email address of this Contact
      */
     public String[] getNote() {
             return notenote;
     }

     /**
      * Returns a JSON encoded version of this contact
      * @return A JSON encoded string representation of this object
      * @throws JSONException 
      */
     public String toJSON() throws JSONException {
    	 JSONObject json = new JSONObject();
         json.put(JSON_NAME_ATTRIBUTE, categoryname);
         json.put(JSON_NOTE_PLACE_ATTRIBUTE, noteplace);
         json.put(JSON_NOTE_NOTE_ATTRIBUTE, notenote);
         json.put(JSON_NOTE_LAT_ATTRIBUTE, notelat);
         json.put(JSON_NOTE_LONG_ATTRIBUTE, notelong);
         return json.toString();
     }

     /** 
      * Creates a new contact by through the provided JSON object
      * @param json JSON encoded string
      * @return Contact instance from json
      * @throws JSONException 
      */
     public static Category fromJSON(String json) throws JSONException {
             
             // Parse JSON
             JSONObject jsonObject = new JSONObject(json);
             String name = jsonObject.getString(JSON_NAME_ATTRIBUTE);
             String[] notePlaces = JSONArrayToString(jsonObject.getJSONArray(JSON_NOTE_PLACE_ATTRIBUTE));
             String[] noteNote = JSONArrayToString(jsonObject.getJSONArray(JSON_NOTE_NOTE_ATTRIBUTE));
             String[] noteLat = JSONArrayToString(jsonObject.getJSONArray(JSON_NOTE_LAT_ATTRIBUTE));
             String[] noteLong = JSONArrayToString(jsonObject.getJSONArray(JSON_NOTE_LONG_ATTRIBUTE));
             
             
             return new Category(name, notePlaces, noteNote, noteLat, noteLong);
     }
     private static String[] JSONArrayToString(JSONArray jsonArray) throws JSONException {
    	 String[] ret = new String[jsonArray.length()];
    	 if (jsonArray != null) { 
    		   for (int i=0;i<jsonArray.length();i++) { 
    			   ret[i] = (jsonArray.getString(i).toString()); 
    		   }
    	 }
    	 return null;
     }

}
